/**
 * Created by Administrator on 2016-05-03.
 */
function open_dialog(url){
    window.open (url, 'newwindow', 'height=500, width=900, top=20, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');
//        window.open (url)
}

function open_tab(url){
    window.open(url)
}

function close_dialog(){
    window.close();
}

function open_window(title, url){
    $('#window').window({
        title:title,
        closable:true,
        modal:true
    });
    $('#window').window('open');
    $('#window_iframe').attr('src', url);
}

//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
    $.messager.alert(title, msgString, msgType);
}

function flowShow(title, msg){
    jQuery.messager.show({title: title,msg: msg});
}

function open_easyui_window_with_id(win_id, win_title, iframe_url){
    $('#window_iframe').attr('src', iframe_url);
    $('#'+win_id).window({
        title:win_title,
        closable:true,
        modal:true
    });
    $('#'+win_id).window('open')
}

function get_ids_from_json(list){
    var str = '';
    for(var i=0;i<list.length;i++){
        var obj = list[i];
        str += obj.id+',';
    }
    return str;
}

function table_index_change(model, value){
    var url = '/Manage/Admin/get_field_array?model='+model+'&table_index='+value;
    $.getJSON(url, function(json){
        var str = '<option></option>';
        for(var i=0;i<json.length;i++){
            var obj = json[i];
            str += '<option value="'+obj.field+'">'+obj.text+'</option>';
        }
        str = '<select id="com_table_field" class="easyui-combobox">'+str+'</select>';
        $('#table_field').empty();
        $('#table_field').append(str);
        $('#com_table_field').combobox({
            width:180,
            onChange: function (newValue, oldValue) {
                table_field_change(model, value, newValue);
            }
        });
    });
}

function table_field_change(model, table_index, value){
    var url = '/Manage/Admin/get_field_array?model='+model+'&table_index='+table_index+'&table_field='+value;
    $.getJSON(url, function(json){
        $('#condition_td').empty();
        var str = '';
        if(json.type == 1){
            //是文本框
            str = '<input name="'+json.field+'" style="width:180px;"/>';
            $('#condition_td').append(str);
        }else if(json.type == 2){
            //是下拉框
            str = '<select name="'+json.field+'" id="select_'+json.field+'" style="width:130px;">'+json.option_text+'</select>';
            $('#condition_td').append(str);
        }else if(json.type == 3){
            str += '<select  name="sysbom">'+json.condition+'</select>&nbsp;属性值：';
            str += '<input name="'+json.field+'" style="width:180px;"/>';
            $('#condition_td').append(str);
        }else if(json.type == 4){
            str += '<select  name="sysbom">'+json.condition+'</select>&nbsp;属性值：';
            str += '<input name="'+json.field+'" id="date_'+json.field+'" style="width:130px;"/>';
            $('#condition_td').append(str);
            $('#date_'+json.field).datebox();
        }
    });
}

function create_search_tag(text, name, value){
    if(value==''||value==undefined){
        set_tag_with_all(name);
    }else{
        $('#'+name+'_').remove();
    }
    if($('#'+name+'_'+value).length > 0){
        return;
    }
    var str = '<div style="" class="tag-item '+name+'_tag" id="'+name+'_'+value+'"><span>'+text+'</span>';
    str += '<a href="#" onclick="remove_search_tag(\''+name+'_'+value+'\')" class="icon_ext icon-delete"></a>';
    str += '<input type="hidden" class="'+name+'_input" value="'+value+'">';
    str +='</div>';
    $('#search_tag_content').append(str);

}

function remove_search_tag(div_id){
    $('#'+div_id).remove();
}

function set_tag_with_all(name){
    $('.'+name+'_tag').remove();
}

function get_search_tag_ids(name){
    var ids = '';
    $('.'+name+'_input').each(function(){
        if($(this).val()!=''){
            ids += $(this).val()+',';
        }
    });
    return ids;
}

function ajaxLoading(){
    $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:$(window).height()}).appendTo("body");
    $("<div class=\"datagrid-mask-msg\"></div>").html("正在处理，请稍候。。。").appendTo("body").css({display:"block",left:($(document.body).outerWidth(true) - 190) / 2,top:($(window).height() - 45) / 2});
}
function ajaxLoadEnd(){
    $(".datagrid-mask").remove();
    $(".datagrid-mask-msg").remove();
}

function add_s_gallery(s_type, img_type, path, content_id, img_id){
    //var data = {'s_type':s_type, 's_id':s_id, 'img_type': img_type, 'path':path};
    //var url = '/Manage/AdminSGallery/add_s_gallery';
    //$.post(url, data, function(res){
    //
    //});
    $('#'+content_id).append(create_s_gallery_img(s_type, img_type, path, img_id));
}

function create_s_gallery_img(s_type, img_type, path, id){
    var  timestamp = new Date().getTime();
    var str = '<div class="s_g_img_div" id="s_g_'+timestamp+'">';
    str += '<img src="'+path+'" onclick="img_show(\''+path+'\')">';
    str += '<input class="s_g_img_input" type="hidden" value="'+s_type+'&'+img_type+'&'+path+'">';
    str += '<div class="s_g_img_tip">';
    str += '<a href="#" onclick="remove_s_gallery_img('+timestamp+', \''+id+'\')" class="icon_ext icon-delete"></a>';
    str += '</div>';
    str += '</div>';
    return str;
}

function remove_s_gallery_img(timestamp, id){
    $('#s_g_'+timestamp).remove();
    if(!id||id==''){
        return;
    }
    var data = {'id':id};
    var url = '/Manage/AdminSGallery/del_s_gallery';
    $.post(url, data, function(res){

    });
}

function get_s_gallery_ids(){
    var ids = '';
    $('.s_g_img_input').each(function(){
        if($(this).val()!=''){
            ids += $(this).val()+',';
        }
    });
    return ids;
}

function hide_s_g_img_tip(){
    $('.s_g_img_tip').remove();
}

function img_show(path){
    $('#img_show_dlg a').attr('href', '/Manage/AdminSGallery/download_img?path='+path);
    $('#img_show_dlg img').attr('src', path);
    $('#img_show_dlg').dialog('open');
}

/**
 * 表单序列化为json对象
 */
(function($){
    $.fn.serializeJson=function(){
        var serializeObj={};
        var array=this.serializeArray();
        //var str=this.serialize();
        $(array).each(function(){
            if(this.value!=''&&this.value!=undefined){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            }

        });
        return serializeObj;
    };
})($);